package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.dtos.WmChannelListDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel>
    implements WmChannelService {

  @Autowired private WmChannelMapper wmChannelMapper;
  @Autowired private WmNewsMapper wmNewsMapper;

  /**
   * 查询所有频道
   *
   * @return
   */
  @Override
  public ResponseResult findAll() {
    return ResponseResult.okResult(list());
  }

  /**
   * 保存频道
   *
   * @param dto
   * @return
   */
  @Override
  public ResponseResult saveChannel(WmChannelDto dto) {
    // 参数校验
    if (ObjectUtils.isEmpty(dto)) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    // 获取频道名称
    String postName = dto.getName();
    if (StringUtils.isEmpty(postName)) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }
    // 根据频道名称查询频道列表
    LambdaQueryWrapper<WmChannel> qw = new LambdaQueryWrapper<>();
    qw.eq(WmChannel::getName, postName);
    WmChannel wmChannel = wmChannelMapper.selectOne(qw);
    if (wmChannel != null) {
      // 频道已存在，不能新增
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    // 新增频道
    wmChannel = new WmChannel();
    BeanUtils.copyProperties(dto, wmChannel);
    wmChannel.setCreatedTime(new Date());
    wmChannel.setIsDefault(true);
    wmChannelMapper.insert(wmChannel);
    return ResponseResult.okResult(200, "操作成功");
  }

  /**
   * 分页模糊查询频道列表
   *
   * @param dto
   * @return
   */
  @Override
  public ResponseResult channelList(WmChannelListDto dto) {
    // 参数校验
    if (ObjectUtils.isEmpty(dto)) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    // 获取分页信息
    Integer postPage = dto.getPage();
    Integer postSize = dto.getSize();
    String postName = dto.getName();
    if (ObjectUtils.isEmpty(postPage)) {
      postPage = 1;
    }
    if (ObjectUtils.isEmpty(postSize)) {
      postSize = 10;
    }
    LambdaQueryWrapper<WmChannel> qw = new LambdaQueryWrapper<>();

    // 设置模糊查询条件
    qw.like((!postName.isEmpty()), WmChannel::getName, postName);

    // 分页条件
    Page<WmChannel> page = new Page<>(postPage, postSize);
    Page<WmChannel> wmChannelPage = wmChannelMapper.selectPage(page, qw);
    List<WmChannel> wmChannelList = wmChannelPage.getRecords();
    return ResponseResult.okResult(wmChannelList);
  }

  /**
   * 修改频道信息
   *
   * @param dto
   * @return
   */
  @Override
  public ResponseResult updateChannel(WmChannelDto dto) {
    // 参数校验
    if (ObjectUtils.isEmpty(dto)) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    // 获取频道属性
    Boolean status = dto.getStatus();
    if (!status) {
      // 如果修改为禁用，则需要判断频道下是否有文章
      LambdaQueryWrapper<WmNews> qw = new LambdaQueryWrapper<>();
      qw.eq(WmNews::getChannelId, dto.getId());
      List<WmNews> wmNewsList = wmNewsMapper.selectList(qw);
      if (!CollectionUtils.isEmpty(wmNewsList)) {
        // 如果有则不能修改为禁用状态
        return ResponseResult.errorResult(1002, "频道下有文章，不能修改为禁用状态");
      }
    }
    // 根据频道id查询
    WmChannel wmChannel = wmChannelMapper.selectById(dto.getId());
    if (ObjectUtils.isEmpty(wmChannel)) {
      // 没有此数据
      return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }
    // 属性拷贝
    BeanUtils.copyProperties(dto, wmChannel);
    // 更新数据库
    wmChannelMapper.updateById(wmChannel);
    return ResponseResult.okResult(200, "操作成功");
  }

  /**
   * 删除频道
   *
   * @param id
   * @return
   */
  @Override
  public ResponseResult delChannel(Integer id) {
    // 校验参数
    if (id == null) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    // 根据id查询频道
    WmChannel wmChannel = wmChannelMapper.selectById(id);
    if (ObjectUtils.isEmpty(wmChannel)) {
      return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }
    // 获取频道状态
    Boolean status = wmChannel.getStatus();
    if (status) {
      // 启用状态不能删除
      return ResponseResult.errorResult(1003, "启动状态不能删除");
    }
    // 删除频道
    wmChannelMapper.deleteById(id);
    return ResponseResult.okResult(200, "操作成功");
  }
}
